// JavaScript Document
$(function() {
    var self = ly.getUrlParam("self");
    var $vettingBox = parent.$('.vetting-iframe.' + self), //获取父级容器
        options = $vettingBox.data(); //设置项
    console.log(options, self);
    var auditId = options.auditId,
        postData = {
            auditId: self
        };
    //加载审批数据
    //$approveListBox 写入的容器
    var loadApproveList = function(postData, $orgListBox) {
			var applyName,pinyin,agency,head;
        request(site.apiUrl + "/audit/getAuditRecordByAuditId", postData, function(data) {
            if (data.code.indexOf('success') >= 0) {
							var audit = data.result.audit,
									auditInfos = data.result.auditInfos;
							var applyEndTime = audit.applyEndTime;
							var thisPicHtml = audit.userPicUrl ? 'style="background-image: url(\'' + audit.userPicUrl + '\')"' : '';
							if (audit.applyMode == 1) {
								applyName = audit.applyOrgName;
								agency = "自主申报";
							}
							else if (audit.applyMode == 2) {
								applyName = audit.applyOrgName||"组织不存在";
								agency = (audit.agencyOrgName||"组织不存在")+" 代";
							}
							else if (audit.applyMode == 3) {
								applyName = audit.applyMemberName||"用户不存在";
								agency = (audit.agencyOrgName||"组织不存在")+" 代";
							}
							else if (audit.applyMode == 4) {
								applyName = audit.applyMemberName||"用户不存在";
								agency = "自主申报";
							}
							if (audit.userPicUrl == "" || audit.userPicUrl == undefined) {
								pinyin = makePy(applyName).join(",")[0];
								head = '<div class="levelName green">' + pinyin + '</div>';
							}
							else {
								head = '<div class="levelName" ' + thisPicHtml + '></div>';
							}
							$('.one-level').html('' +
									 head +
									'<span class="dotteds borderGreen"></span>' +
									'<div class="details fr">' +
									'<span class="statusStart fontGreen pl10">发起</span>' +
									'<span class="orgName pl10">' + applyName+ '</span>' +
									'<span class="adminName pl10">' + agency + '</span>' +
									'<span class="approveDate fr">' + audit.applyStartTime + '(耗时' + getInterval(audit.applyStartTime, applyEndTime) + ')</span>' +
									'</div>' +
									'<span class="remark pl10">' + audit.applyTheme + '</span>');

                var thisnum = (auditInfos.length) - 1;
                var lastTime = auditInfos.length != 0 ? auditInfos[thisnum].auditTime : '';
                    elapsedTime = auditInfos.length != 0 ? getInterval(audit.applyStartTime, auditInfos[thisnum].auditTime) : '';
                
                $('.footer').html('' +
                    '<span class="staimg"></span>' +
                    '<div class="footerdetails fr">' +
                    '<span class="statusEnd pl10">' + audit.auditStateDisplay + '</span>' +
                    '<span class="approveTime pl10">审批期限：' + getInterval(audit.applyStartTime, applyEndTime) + '，截至时间：' + applyEndTime + '</span>' +
                    '<span class="approveDate fr">' + lastTime + '(耗时' + elapsedTime + ')</span>');
                
                var thisProcess = audit.auditProcessDisplay ? audit.auditProcessDisplay : '逐级审批';
                if (audit.auditState == 2) {
                    $('.stasection').addClass('pass');
                    $('.staimg').addClass('passGreen');
                    $('.statusEnd').addClass('fontGreen');
                    $('.headline').html('审批通过-' + thisProcess);
                } else if (audit.auditState == 4) {
                    $('.stasection').addClass('back');
                    $('.staimg').addClass('backYellow');
                    $('.statusEnd').addClass('fontYellow');
                    $('.headline').html('审批退回-' + thisProcess);
                } else if (audit.auditState == 5) {
                    $('.stasection').addClass('Recall');
                    $('.staimg').addClass('recallBlue');
                    $('.statusEnd').addClass('fontBlue');
                    $('.headline').html('审批撤回-' + thisProcess);
                } else if (audit.auditState == 1) {
                    $('.headline').html('审批中-' + thisProcess);
                    $('.staimg').addClass('backYellow');
                    $('.statusEnd').addClass('fontYellow');
                    $('.footer').find('.approveDate').addClass('hide');
                } else if (audit.auditState == 7) {
                    $('.headline').html('审批中-' + thisProcess);
                    $('.staimg').addClass('backYellow');
                    $('.statusEnd').addClass('fontYellow');
                }
                var firstTi, secondTi;
                var hasdata = audit.auditPathName.split(','),
                    hasnum = hasdata.length,
                    shownum = auditInfos.length;
                $.each(hasdata, function(i, j) {
                    var item = auditInfos[i];
                    var h = '';
                    console.log(item)
                    if (item != undefined) {
                        if (i == 0) {
                            firstTi = audit.applyStartTime;
                            secondTi = item.auditTime;
                        } else {
                            var ind = i - 1;
                            firstTi = auditInfos[ind].auditTime;
                            secondTi = item.auditTime;
                        }
                        var auditorName = item.auditOrgName ? item.auditOrgName : "组织不存在";
                        var auditOrgTypeDisplay = item.auditOrgTypeDisplay ? item.auditOrgTypeDisplay : '党委';
                        var thisHeadHtml = item.orgPicUrl ? 'style="background-image: url(\'' + item.orgPicUrl + '\')"' : '';
//                        if (item.auditComment == '系统自动审批') {
//                            auditorName = '';
//                        }
											
												if (!thisHeadHtml) {
													pinyin = makePy(auditorName).join(",")[0];
													head = '<div class="levelName">' + pinyin + '</div>';
												}
												else {
													head = '<div class="levelName" ' + thisHeadHtml + '></div>';
												}
												h = '' +
														'<div class="two-level appro' + i + '">' +
														head +
														'<span class="dotteds"></span>' +
														'<div class="details fr">' +
														'<span class="status pl10">' + item.auditStateDisplay + '</span>' +
//														'<span class="adminName pl10">' + auditorName + '</span>' +
														'<span class="orgName pl10">' + auditorName + '</span>' +
														'<span class="approveDate fr">' + item.auditTime + '(耗时' + getInterval(firstTi, secondTi) + ')</span>' +
														'</div>' +
														'<span class="remark pl10">' + item.auditComment + '</span>' +
														'</div>';
                        var lastd = hasnum - 1;
                        if (item.auditStateDisplay != '通过' && item.auditOrgName == hasdata[lastd]) {
                            $('.last').append(h);
                        } else {
                            $('.third-level').append(h);
                        }

                        var status = item.auditStateDisplay;
                        if (status == '通过') {
                            $('.appro' + i).find('.levelName').addClass('green');
                            $('.appro' + i).find('.dotteds').addClass('borderGreen');
                            $('.appro' + i).find('.status').addClass('fontGreen');
                        } else if (status == '拒绝') {
                            $('.appro' + i).find('.levelName').addClass('red');
                            $('.appro' + i).find('.dotteds').addClass('borderRed');
                            $('.appro' + i).find('.status').addClass('fontRed');
                        } else if (status == '待审批') {
                            $('.appro' + i).find('.levelName').addClass('yellow');
                            $('.appro' + i).find('.dotteds').addClass('borderYellow');
                            $('.appro' + i).find('.status').addClass('fontYellow');
                        } else if (status == '未审批') {
                            $('.appro' + i).find('.levelName').addClass('blue');
                            $('.appro' + i).find('.dotteds').addClass('borderBlue');
                            $('.appro' + i).find('.status').addClass('fontBlue');
                        } else if (status == '不通过') {
                            $('.appro' + i).find('.levelName').addClass('yellow');
                            $('.appro' + i).find('.dotteds').addClass('borderYellow');
                            $('.appro' + i).find('.status').addClass('fontGray');
                        }
                    } else {
                        var thisOrgName = hasdata[shownum];
                        var thisSpell = makePy(hasdata[i]).join(",")[0];
                        var prevnum = shownum - 1;
                        // if (audit.auditStateDisplay != '审批中') return;
                        if (i == shownum) {
                            if (audit.auditStateDisplay != '审批中') {
                                $('.third-level').append('' +
                                    '<div class="two-level">' +
                                    '<div class="levelName yellow">' + thisSpell + '</div>' +
                                    '<span class="dotteds borderYellow"></span>' +
                                    '<div class="details fr">' +
                                    '<span class="status pl10 fontYellow">未审批</span>' +
                                    '<span class="adminName pl10"></span>' +
                                    '<span class="orgName pl10">' + j + '</span>' +
                                    '<span class="approveDate fr"></span>' +
                                    '</div>' +
                                    '<span class="remark pl10"></span>' +
                                    '</div>');
                            } else {
                                $('.third-level').append('' +
                                    '<div class="two-level">' +
                                    '<div class="levelName yellow">' + thisSpell + '</div>' +
                                    '<span class="dotteds borderYellow"></span>' +
                                    '<div class="details fr">' +
                                    '<span class="status pl10 fontYellow">待审批</span>' +
                                    '<span class="adminName pl10"></span>' +
                                    '<span class="orgName pl10">' + j + '</span>' +
                                    '<span class="approveDate fr"></span>' +
                                    '</div>' +
                                    '<span class="remark pl10"></span>' +
                                    '</div>');
                            }
                        } else {
                            
                                $('.third-level').append('' +
                                    '<div class="two-level">' +
                                    '<div class="levelName yellow">' + thisSpell + '</div>' +
                                    '<span class="dotteds borderYellow"></span>' +
                                    '<div class="details fr">' +
                                    '<span class="status pl10 fontYellow">未审批</span>' +
                                    '<span class="adminName pl10"></span>' +
                                    '<span class="orgName pl10">' + j + '</span>' +
                                    '<span class="approveDate fr"></span>' +
                                    '</div>' +
                                    '<span class="remark pl10"></span>' +
                                    '</div>');
                            
                        }
                        $('.third-level').append(h);
                    }
                });

                var thisH = $('.content').height();
                $vettingBox.css('height', thisH);
            }
        }, 'POST');
    };
    loadApproveList(postData, $('.approveList'));
    //获取时间间隔
    var getInterval = function(startTime, endTime) {
        var startDate = startTime.replace(/-/g, '/'),
            endDate = endTime.replace(/-/g, '/'),
            timestamp1 = new Date(startDate).getTime(),
            timestamp2 = new Date(endDate).getTime(),
            inter = timestamp2 - timestamp1,
            txt = '';
        if (inter >= 0) {
            var d = Math.floor(inter / 1000 / 60 / 60 / 24),
                h = Math.floor(inter / 1000 / 60 / 60 % 24),
                m = Math.floor(inter / 1000 / 60 % 60),
                s = Math.floor(inter / 1000 % 60);
        }
        txt = '' + d + '天' + h + '小时' + m + '分钟' + s + '秒';
        if (d == 0) {
            txt = '' + h + '小时' + m + '分钟' + s + '秒';
        }
        if (d == 0 && h == 0) {
            txt = '' + m + '分钟' + s + '秒';
        }
        if (d == 0 && h == 0 && m == 0) {
            txt = '' + s + '秒';
        }
        return txt;
    };
});

//组件调用
// $.fn.vetting = function(options) {
//     //没有auditId，无法进行后面的操作，提示用户orgCode不能为空
//     if (!options.auditId) {
//         ly.toast("auditId不能为空");
//         return;
//     }
//     var _this = $(this);
//     _this.html('<iframe class="vetting-iframe" style="width:100%;height:100%;" src="../../js/vetting/index.html" frameborder="0" border="0"></iframe>');
//     //设置项存储
//     $('.vetting-iframe').data(options);
// };
